package com.shiro.receiver.framework.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.Ordered;
import com.shiro.receiver.framework.annotation.Filter;
import com.shiro.receiver.framework.servlet.RepeatServletRequest;

import java.io.IOException;
import java.util.List;

@Filter
public class RepeatServletRequestFilter implements WebFilter {

    @Override
    public String getName() {
        return "可重复读Request过滤器";
    }

    @Override
    public Integer getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public List<String> getPathPatterns() {
        return List.of("/**");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest httpServletRequest) {
            RepeatServletRequest repeatServletRequest = new RepeatServletRequest(httpServletRequest);
            filterChain.doFilter(repeatServletRequest, servletResponse);
        }
        else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

}
